package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.ClueDto;
import com.qk.dto.MarkFalseClueDto;
import com.qk.entity.Clue;
import com.qk.service.ClueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/clues")
public class ClueController {

    @Autowired
    private ClueService clueService;

    //新增线索
    @PostMapping
    public Result add(@RequestBody Clue  clue){
        log.info("新增线索:{}",clue);

        clue.setCreateTime(LocalDateTime.now());
        clue.setUpdateTime(LocalDateTime.now());
        clue.setStatus(1);
        clueService.save(clue);
        return Result.success();

    }

    //分页列表查询
    @GetMapping
    public Result list(ClueDto clueDto){
        log.info("列表查询:{}",clueDto);
        PageResult<Clue> pageResult = clueService.listClues(clueDto);
        return Result.success(pageResult);
    }

    //分配线索
    @PutMapping("/assign/{clueId}/{userId}")
    public Result assign(@PathVariable Integer clueId,@PathVariable Integer userId){
        log.info("分配线索:{},{}",clueId,userId);
        //new对象
        Clue clue = new Clue();
        clue.setId(clueId);
        clue.setUserId(userId);
        clue.setStatus(2);//待跟进
        clue.setUpdateTime(LocalDateTime.now());
        //直接调用 service,简单SQL语句
        clueService.updateById(clue);
        return Result.success();
    }

    //根据ID查询回显
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询id:{}",id);
        Clue clue = clueService.getClueById(id);
        return Result.success(clue);
    }

    //线索跟进
    @PutMapping
    public Result trackClue(@RequestBody Clue  clue) {
        log.info("线索跟进:{}",clue);
        clueService.trackClue(clue);
        return Result.success();
    }

    //伪线索
    @PutMapping("/false/{id}")
    public Result fake(@PathVariable Integer id,@RequestBody MarkFalseClueDto markFalseClueDto) {
        log.info("伪线索:{},{}", id, markFalseClueDto);
        //添加跟进记录
        clueService.markFalseClue(id,markFalseClueDto);
        return Result.success();
    }

    //转商机
    @PutMapping("/toBusiness/{id}")
    public Result toBusiness(@PathVariable Integer id){
        log.info("转商机:{}",id);
        clueService.toBusiness(id);
        return Result.success();
    }

    //线索池列表查询
    @GetMapping("/pool")
    public Result listPool(ClueDto clueDto){//查询参数之前什么注解都不用加
        log.info("线索池列表查询:{}",clueDto);
        //调用Service查询的时候涉及到多表查询,所以起名不能与Mybatisplus的方法重名
        //分页查询雷打不动返回一个PageResult,要返回的是List列表和总数,PageResult封装了List列表和总数
        PageResult<Clue> pageResult = clueService.Clues(clueDto);
        //返回结果
        return Result.success(pageResult);

    }

}
